<?php

namespace iBrand\Shop\Backend\Service;

use iBrand\Component\Goods\Models\Style;
use iBrand\Component\Goods\Models\Season;
use iBrand\Component\Purchase\Repositories\TakeStockItemRepository;
use iBrand\Component\Purchase\Repositories\TakeStockRepository;
use DB;

class TakeStockService
{
	protected $takeStockItemRepository;
	protected $takeStockRepository;

	public function __construct(TakeStockItemRepository $takeStockItemRepository, TakeStockRepository $takeStockRepository)
	{
		$this->takeStockItemRepository = $takeStockItemRepository;
		$this->takeStockRepository     = $takeStockRepository;
	}

	public function getStoreRecordDetailList($where, $time, $stock, $item_ids, $product_ids, $limit = 20)
	{
		$list = $this->takeStockItemRepository->scopeQuery(function ($query) use ($where, $time, $stock, $item_ids, $product_ids) {
			if (!empty($where)) {
				foreach ($where as $key => $value) {
					if (is_array($value)) {
						list($operate, $va) = $value;
						$query = $query->where($key, $operate, $va);
					} else {
						$query = $query->where($key, $value);
					}
				}
			}

			if (!empty($time)) {
				foreach ($time as $key => $value) {
					if (is_array($value)) {
						list($operate, $va) = $value;
						$query = $query->where($key, $operate, $va);
					} else {
						$query = $query->where($key, $value);
					}
				}
			}

			if (isset($stock['shop_id']) && $stock['shop_id']) {
				$shop_id = $stock['shop_id'];
				$query   = $query->whereHas('takeStock', function ($query) use ($shop_id) {
					return $query->where('shop_id', $shop_id);
				});
			}

			if (isset($stock['clerk_id']) && $stock['clerk_id']) {
				$clerk_id = $stock['clerk_id'];
				$query    = $query->whereHas('takeStock', function ($query) use ($clerk_id) {
					return $query->where('clerk_id', $clerk_id);
				});
			}

			if (isset($stock['code']) && $stock['code']) {
				$code  = $stock['code'];
				$query = $query->whereHas('takeStock', function ($query) use ($code) {
					return $query->where('code', $code[0], $code[1]);
				});
			}

			if (!empty($item_ids)) {
				$query = $query->whereIn('item_id', $item_ids);
			}

			if (!empty($product_ids)) {
				$query = $query->whereIn('item_id', $product_ids);
			}

			return $query->with('takeStock')->orderBy('created_at', 'DESC');
		});

		if ($limit) {
			$list = $list->paginate($limit);
		} else {
			$list = $list->all();
		}

		foreach ($list as $item) {
			$item->orderNo      = $item->takeStock->code;
			$item->goodsName    = $item->meta['goods_name'];
			$item->goodsSn      = $item->meta['goods_no'];
			$item->colorName    = $item->meta['color'];
			$item->sizeName     = $item->meta['size'];
			$item->categoryName = $item->product->goods->categories()->orderBy('id', 'DESC')->first()->id;
			$item->blandName    = $item->product->goods->brand->id;
			$item->styleName    = Style::where('id', $item->product->goods->style_id)->value('name');
			$item->seasonName   = Season::where('id', $item->product->goods->season_id)->value('name');
			$item->year         = $item->product->goods->year;
			$item->number       = $item->count;
			$item->beforeNumber = $item->before_count;
			$item->profitNumber = $item->difference_count;
			$item->profitAmount = $item->difference_amount;
			$item->userName     = $item->takeStock->clerk->name;
			$item->updateTime   = date('Y-m-d H:i:s', strtotime($item->created_at));
		}

		return $list;
	}

	public function getStoreRecordDetailExportData($list)
	{
		$data = [];
		if ($list && $list->count() > 0) {
			$i = 0;
			foreach ($list as $item) {
				$data[$i][] = $item->takeStock->code;
				$data[$i][] = $item->meta['goods_name'];
				$data[$i][] = $item->meta['goods_no'];
				$data[$i][] = $item->meta['color'];
				$data[$i][] = $item->meta['size'];
				$data[$i][] = $item->product->goods->categories()->orderBy('id', 'DESC')->first()->name;
				$data[$i][] = $item->product->goods->brand->name;
				$data[$i][] = Style::where('id', $item->product->goods->style_id)->value('name');
				$data[$i][] = Season::where('id', $item->product->goods->season_id)->value('name');
				$data[$i][] = $item->product->goods->year;
				$data[$i][] = $item->count;
				$data[$i][] = $item->before_count;
				$data[$i][] = $item->difference_count;
				$data[$i][] = $item->difference_amount;
				$data[$i][] = $item->takeStock->clerk->name;
				$data[$i][] = date('Y-m-d H:i:s', strtotime($item->created_at));
				$i++;
			}
		}

		return $data;
	}

	public function getStoreRecordDetailExportTitle()
	{
		return ['单号', '商品名称', '货号', '颜色', '尺码', '分类', '品牌', '款式', '季节', '年份', '盘点数量', '盘点前数量', '盈亏数量', '盈亏金额', '盘点人', '盘点时间'];
	}

	public function getStoreRecordGroupByOrderList($where, $time, $limit = 20)
	{
		$list = $this->takeStockRepository->getTakeStockPaginate($where, $time, $limit);

		foreach ($list as $item) {
			$item->orderNo      = $item->code;
			$item->number       = $item->total;
			$item->beforeNumber = $item->items()->sum('before_count');
			$item->profitNumber = $item->difference_count;
			$item->profitAmount = $item->difference_amount;
			$item->userName     = $item->clerk->name;
			$item->updateTime   = date('Y-m-d H:i:s', strtotime($item->created_at));
		}

		return $list;
	}

	public function getStoreRecordGroupByOrderExportData($list)
	{
		$data = [];
		if ($list && $list->count() > 0) {
			$i = 0;
			foreach ($list as $item) {
				$data[$i][] = $item->code;
				$data[$i][] = $item->total;
				$data[$i][] = $item->items()->sum('before_count');
				$data[$i][] = $item->difference_count;
				$data[$i][] = $item->difference_amount;
				$data[$i][] = $item->clerk->name;
				$data[$i][] = date('Y-m-d H:i:s', strtotime($item->created_at));
				$i++;
			}
		}

		return $data;
	}

	public function getStoreRecordGroupByOrderExportTile()
	{
		return ['单号', '盘点数量', '盘点数量', '盈亏数量', '盈亏金额', '盘点人', '盘点时间'];
	}

	public function getStoreRecordGroupByGoodList($where, $time, $stock, $goods_ids, $limit = 20)
	{
		$list = $this->takeStockItemRepository->scopeQuery(function ($query) use ($where, $time, $stock, $goods_ids) {
			if (!empty($where)) {
				foreach ($where as $key => $value) {
					if (is_array($value)) {
						list($operate, $va) = $value;
						$query = $query->where($key, $operate, $va);
					} else {
						$query = $query->where($key, $value);
					}
				}
			}

			if (!empty($time)) {
				foreach ($time as $key => $value) {
					if (is_array($value)) {
						list($operate, $va) = $value;
						$query = $query->where($key, $operate, $va);
					} else {
						$query = $query->where($key, $value);
					}
				}
			}

			if (isset($stock['shop_id']) && $stock['shop_id']) {
				$shop_id = $stock['shop_id'];
				$query   = $query->whereHas('takeStock', function ($query) use ($shop_id) {
					return $query->where('shop_id', $shop_id);
				});
			}

			if (!empty($goods_ids)) {
				$query = $query->whereIn('goods_id', $goods_ids);
			}

			return $query->groupBy('goods_id')->orderBy('created_at', 'DESC');
		});

		if ($limit) {
			$list = $list->paginate(20, ['*', DB::raw('sum(count) as total_count'), DB::raw('sum(before_count) as total_before_count'), DB::raw('sum(difference_count) as total_difference_count'), DB::raw('sum(difference_amount) as total_difference_amount')]);
		} else {
			$list = $list->all(['*', DB::raw('sum(count) as total_count'), DB::raw('sum(before_count) as total_before_count'), DB::raw('sum(difference_count) as total_difference_count'), DB::raw('sum(difference_amount) as total_difference_amount')]);
		}

		foreach ($list as $item) {
			$item->goodsName    = $item->meta['goods_name'];
			$item->goodsSn      = $item->meta['goods_no'];
			$item->colorName    = $item->meta['color'];
			$item->sizeName     = $item->meta['size'];
			$item->categoryName = $item->product->goods->categories()->orderBy('id', 'DESC')->first()->id;
			$item->blandName    = $item->product->goods->brand->id;
			$item->styleName    = Style::where('id', $item->product->goods->style_id)->value('name');
			$item->seasonName   = Season::where('id', $item->product->goods->season_id)->value('name');
			$item->year         = $item->product->goods->year;
			$item->number       = $item->total_count;
			$item->beforeNumber = $item->total_before_count;
			$item->profitNumber = $item->total_difference_count;
			$item->profitAmount = $item->total_difference_amount;
		}

		return $list;
	}

	public function getStoreRecordGroupByGoodExportData($list)
	{
		$data = [];
		if ($list && $list->count() > 0) {
			$i = 0;
			foreach ($list as $item) {
				$data[$i][] = $item->meta['goods_name'];
				$data[$i][] = $item->meta['goods_no'];
				$data[$i][] = $item->product->goods->categories()->orderBy('id', 'DESC')->first()->name;
				$data[$i][] = $item->product->goods->brand->name;
				$data[$i][] = Style::where('id', $item->product->goods->style_id)->value('name');
				$data[$i][] = Season::where('id', $item->product->goods->season_id)->value('name');
				$data[$i][] = $item->product->goods->year;
				$data[$i][] = $item->total_count;
				$data[$i][] = $item->total_before_count;
				$data[$i][] = $item->total_difference_count;
				$data[$i][] = $item->total_difference_amount;
				$i++;
			}
		}

		return $data;
	}

	public function getStoreRecordGroupByGoodExportTitle()
	{
		return ['商品名称', '货号', '分类', '品牌', '款式', '季节', '年份', '盘点数量', '盘点前数量', '盈亏数量', '盈亏金额'];
	}
}